package com.boc.langxiang.ui.fragment.adm;


import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;

import com.boc.langxiang.Constant;
import com.boc.langxiang.R;
import com.boc.langxiang.contract.adm.ArtistAboutContract;
import com.boc.langxiang.entity.request.AdmAboutRequest;
import com.boc.langxiang.entity.response.AdmAboutEntity;
import com.boc.langxiang.entity.response.ArticleEntity;
import com.boc.langxiang.presenter.adm.ArtistAboutPresenter;
import com.boc.langxiang.ui.activity.index.ArticleDetailActivity;
import com.boc.langxiang.ui.adapter.adm.ArtistAboutAdapter;
import com.boc.langxiang.ui.fragment.BaseFragment;
import com.boc.langxiang.util.T;
import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 艺术家-关于
 */
public class ArtistAboutFragment extends BaseFragment implements SwipeRefreshLayout.OnRefreshListener,
        ArtistAboutContract.View, BaseQuickAdapter.RequestLoadMoreListener {
    @BindView(R.id.recylerview)
    RecyclerView mRecylerview;
    @BindView(R.id.swipeLayout)
    SwipeRefreshLayout mSwipeLayout;
    private ArtistAboutContract.Presenter mPresenter;
    private ArtistAboutAdapter mAdapter;
    private boolean mIsOnRefresh;
    private int mPage = 1;
    private String designerId;
    private AdmAboutRequest mAdmAboutRequest;
    private boolean mIsLoadmore;

    public static ArtistAboutFragment newInstance(String id) {
        ArtistAboutFragment fragment = new ArtistAboutFragment();
        fragment.designerId = id;
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_artist_about, container, false);
        ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        mSwipeLayout.setOnRefreshListener(this);
        mSwipeLayout.setColorSchemeColors(Color.RED, Color.GREEN, Color.BLUE, Color.YELLOW);
        mRecylerview.setHasFixedSize(true);
        mRecylerview.setLayoutManager(new LinearLayoutManager(mContext));
        mRecylerview.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (mIsOnRefresh) {
                    return true;
                }
                return false;
            }
        });

        initAdapter();

        mPresenter = new ArtistAboutPresenter(this);
        admAbout();
    }

    private void admAbout() {
        mAdmAboutRequest = new AdmAboutRequest();
        mAdmAboutRequest.designerId = designerId;
        mAdmAboutRequest.page = mPage;
        mPresenter.admAbout(mAdmAboutRequest);
    }

    private void initAdapter() {
        mAdapter = new ArtistAboutAdapter(mContext, null);
        View emptyView = LayoutInflater.from(mContext).inflate(R.layout.view_empty, (ViewGroup) mRecylerview.getParent(), false);
        mAdapter.setEmptyView(emptyView);
        mAdapter.openLoadAnimation();
        //控制itemview动画是否只在第一次显示
        mAdapter.isFirstOnly(false);
        mAdapter.setOnLoadMoreListener(this);
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mRecylerview.setAdapter(mAdapter);
        mAdapter.setOnRecyclerViewItemClickListener(new BaseQuickAdapter.OnRecyclerViewItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                AdmAboutEntity item = mAdapter.getItem(position);
                Intent intent = new Intent(mContext, ArticleDetailActivity.class);
                ArticleEntity data = new ArticleEntity();
                data.setId(item.getId()+"");
                data.setTitle(item.getTitle());
                data.setContent(item.getContent());
                intent.putExtra("data", data);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onRefresh() {
        if (mIsOnRefresh || mIsLoadmore) {
            mSwipeLayout.setRefreshing(false);
            return;
        }
        mAdapter.openLoadMore(Constant.PAGE_SIZE, true);
        mIsOnRefresh = true;
        mPage = 1;
        admAbout();
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
        mSwipeLayout.setRefreshing(false);
    }

    @Override
    public void onError(String code, String msg) {
        mIsLoadmore = false;
        mAdapter.notifyDataChangedAfterLoadMore(false);
        mIsOnRefresh = false;
        T.showShort(mContext, msg);
    }

    @Override
    public void onLoadMoreRequested() {
        mRecylerview.post(new Runnable() {
            @Override
            public void run() {
                if (mIsOnRefresh || mIsLoadmore) {
                    return;
                }
                mIsLoadmore = true;
                mPage++;
                admAbout();
            }
        });
    }

    @Override
    public void admAboutSuccess(List<AdmAboutEntity> admAboutEntitys) {
        if (!isAdded()) {
            return;
        }
        if (mIsLoadmore) {
            mIsLoadmore = false;
            if (admAboutEntitys == null) {
                mAdapter.notifyDataChangedAfterLoadMore(false);
                loadDone();
                return;
            }
            if (admAboutEntitys.size() < Constant.PAGE_SIZE) {
                mAdapter.notifyDataChangedAfterLoadMore(admAboutEntitys, false);
                loadDone();
            } else {
                mAdapter.notifyDataChangedAfterLoadMore(admAboutEntitys, true);
            }

        } else {
            mIsOnRefresh = false;
            if (admAboutEntitys == null) {
                return;
            }
            mAdapter.setNewData(admAboutEntitys);
        }
    }

    private void loadDone() {
        View view = LayoutInflater.from(mContext).inflate(R.layout.view_load_done, (ViewGroup) mRecylerview.getParent(), false);
        mAdapter.addFooterView(view);
    }

}
